package org.example.backend.demos.web;

import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;

import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Date;
import java.util.UUID;

public class JwtTokenUtilOne {

    public static void main(String[] args) {
//        try {
//            String secretKey = JwtTokenUtil.generateSecretKey();
//            System.out.println("Generated Secret Key: " );
//            System.out.println(secretKey);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        //生成加密密码
//        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        System.out.println(encoder.encode("pass123"));

//        String coco = generateToken("coco");
//        System.out.println(coco);
        System.out.println("---------------------------------");
        String claims = parseToken("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJjb2NvIiwiZXhwIjoxNzYwOTYxMTMxfQ.b22wOB7_La6Cq-A9Fj4LaOitkEp_OqnFliGa3BczOJs").toString();
        System.out.println(claims);
    }
//    public static void JWT_TEST(){
//        Key signingKey = Keys.secretKeyFor(SignatureAlgorithm.HS256);
//        String username = "coco";
//        long expire = 1000 * 60 * 5;
//        String token = Jwts.builder()
//
//                .setHeaderParam("typ","JWT")
//                .setHeaderParam("alg","HS256")
//                .setId(UUID.randomUUID().toString())
//                .setIssuedAt(new Date())
//                .setExpiration(new Date(System.currentTimeMillis()+ expire))
//                .setSubject(username)
//                .signWith(signingKey,SignatureAlgorithm.forName("dawdadawdwawdwd"))
//                .compressWith(CompressionCodecs.GZIP)
//                .compact();
//        System.out.println(token);
//    }
//    public static void PARSE_JWT(){
//        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsInppcCI6IkdaSVAifQ.H4sIAAAAAAAA_zXMMQqAMAwF0LtkbqDR1ny9jbYp1EVBBUG8uzq4vuFdNO-VBgKiaJPABvMcehTuvUWOUmKWCbm1hhzVcadBtPPQFgpHdq4_BPlgO6a3S0ta6H4ADb6OlF0AAAA.FyH0gYXH-0UldaxZ90wXE0M2jyFUyQ6ptcgWk6LslAE";
//        String body = Jwts
//                .parserBuilder()
//                .setSigningKey(getSigningKey())
//                .build()
//                .parseClaimsJwt(token)
//                .toString();
//        System.out.println(body);
//    }


    private static final String SECRET_KEY = "my-secure-secret-key-with-sufficient-length-for-hs256-algorithm";
    private static final Key signingKey = Keys.hmacShaKeyFor(SECRET_KEY.getBytes(StandardCharsets.UTF_8));

    public static String generateToken(String username) {

        return Jwts.builder()
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                .setId(UUID.randomUUID().toString())
                .setIssuedAt(new Date())
                .setSubject(username)
                .signWith(signingKey, SignatureAlgorithm.HS256)  // 明确指定使用 HS256 算法
                .setExpiration(new Date(System.currentTimeMillis() + 86400000))
                .compact();

    }

    public static Claims parseToken(String token) {
        return Jwts.parserBuilder()
                .setSigningKey(signingKey)
                .build()
                .parseClaimsJws(token)
                .getBody();
    }
}


